import { Sprite } from "@/sprites/Sprite.js";
import { ImageManager } from "@/managers/ImageManager.js";

export class Sprite_StateOverlay extends Sprite {
    constructor() {
        super();
        this._battler = null;
        this._overlayIndex = 0;
        this._lastOverLayIndex=0;
        this._animationCount = 0;
        this._pattern = 0;
        this.anchor.x = 0.5;
        this.anchor.y = 1;
        this.loadSource();
    }

    loadSource() {
        ImageManager.loadSystem('States').then(source=>{
            this.source=source;
            this.updateFrame();
        });
    }

    setup(battler) {
        this._battler = battler;
    }

    update() {
        super.update();
        this._animationCount++;
        if (this._animationCount >= 8) {
            this.updatePattern();
            this.updateFrame();
            this._animationCount = 0;
        }
    }

    updatePattern() {
        if (this._battler) {
            this._pattern=(this._pattern+1)% 8;
            this._overlayIndex=this._battler.stateOverlayIndex();
        }
    }

    updateFrame() {
        this.visible=false;
        if (this._overlayIndex > 0) {
            let w = 96;
            let h = 96;
            let sx = this._pattern * w;
            let sy = (this._overlayIndex - 1) * h;
            this.setFrame(sx, sy, w, h);
            this.visible=true;
        }
    }
}